package com.ruoyi.web.controller.module.titleMenu;

import java.util.List;
import java.util.Arrays;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.OkHttpClientUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.product.Product;
import com.ruoyi.system.domain.module.titleMenu.TitleMenu;
import com.ruoyi.system.service.module.titleMenu.ITitleMenuService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 首页菜单Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "首页菜单控制器", tags = {"首页菜单管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/titleMenu")
public class TitleMenuController extends BaseController {

    private final ITitleMenuService titleMenuService;

    @Value("${ruoyi.imgUrl}")
	private String imgUrl;

	@Value("${ruoyi.apiUrl}")
	private String apiUrl;

    /**
     * 查询首页菜单列表
     */
    @ApiOperation("查询首页菜单列表")
    @PreAuthorize("@ss.hasPermi('module:menus:list')")
    @GetMapping("/list")
    public TableDataInfo<TitleMenu> list(@Validated TitleMenu titleMenu) {
		QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
		if(titleMenu.getIsShowFirst() !=null){
			if(titleMenu.getIsShowFirst() == 1){
				qw.eq("is_show_first",titleMenu.getIsShowFirst());
			}
		}
		if(titleMenu.getCategoryType() !=null){
			qw.eq("category_type",titleMenu.getCategoryType());
		}
		if(titleMenu.getMenuType() !=null){
			qw.eq("menu_type",titleMenu.getMenuType());
		}
		qw.orderByAsc("sort");
		TableDataInfo<TitleMenu> td = PageUtils.buildDataInfo(titleMenuService.page(PageUtils.buildPage(),qw));
		List<TitleMenu> list = td.getRows();
		if(CollUtil.isNotEmpty(list)){
			list.forEach(titleMenu1 -> {
                if(StrUtil.isNotEmpty(titleMenu1.getLinkLogo())){
                    if(StrUtil.isNotEmpty(titleMenu1.getLinkLogo()) && titleMenu1.getLinkLogo().indexOf("http") < 0){
                        titleMenu1.setImgPath(imgUrl + titleMenu1.getLinkLogo());
                    }else{
                        titleMenu1.setImgPath(titleMenu1.getLinkLogo());
                    }
                }else{
                    titleMenu1.setImgPath("");
                }
			});
		}
        return td;
    }

    /**
     * 获取首页菜单详细信息
     */
    @ApiOperation("获取首页菜单详细信息")
    @PreAuthorize("@ss.hasPermi('module:menus:query')")
    @GetMapping("/{id}")
    public AjaxResult<TitleMenu> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
    	TitleMenu titleMenu = titleMenuService.getById(id);
    	if(titleMenu != null) {
    		if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                if(StrUtil.isNotEmpty(titleMenu.getLinkLogo()) && titleMenu.getLinkLogo().indexOf("http") < 0){
                    titleMenu.setImgPath(imgUrl + titleMenu.getLinkLogo());
                }else{
                    titleMenu.setImgPath(titleMenu.getLinkLogo());
                }
			}else{
				titleMenu.setImgPath("");
			}
		}
        return AjaxResult.success(titleMenu);
    }

    /**
     * 新增首页菜单
     */
    @ApiOperation("新增首页菜单")
    @PreAuthorize("@ss.hasPermi('module:menus:add')")
    @Log(title = "首页菜单", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody TitleMenu titleMenu) {
    	boolean bl = titleMenuService.save(titleMenu);
		if(bl){
			OkHttpClientUtil.getInstance().getData(apiUrl + "redis/reset/menu/" + titleMenu.getMenuType());
			OkHttpClientUtil.getInstance().getData(apiUrl + "redis/reset/title/menu/" + titleMenu.getCategoryType());
		}
        return toAjax(bl ? 1 : 0);
    }

    /**
     * 修改首页菜单
     */
    @ApiOperation("修改首页菜单")
    @PreAuthorize("@ss.hasPermi('module:menus:edit')")
    @Log(title = "首页菜单", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody TitleMenu titleMenu) {
		boolean bl = titleMenuService.updateById(titleMenu);
		if(bl){
			OkHttpClientUtil.getInstance().getData(apiUrl + "redis/reset/menu/" + titleMenu.getMenuType());
			OkHttpClientUtil.getInstance().getData(apiUrl + "redis/reset/title/menu/" + titleMenu.getCategoryType());
		}
        return toAjax(bl ? 1 : 0);
    }

    /**
     * 删除首页菜单
     */
    @ApiOperation("删除首页菜单")
    @PreAuthorize("@ss.hasPermi('module:menus:remove')")
    @Log(title = "首页菜单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(titleMenuService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
